<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ConsulController extends Controller
{
    public function getAdd(){

    return view('Admin.consul.consult');
    }

    //资讯数据的添加
    public function postAdd(Request $request){

   		$res = $request->only('title','content','text');
   		
   		

   		//资讯对上传图片的处理
   		if($request->hasFile('img')){

   			$name = rand(1,9999).time();

   			$suffix=$request->file('img')->getClientOriginalExtension();

   			$arr=array('jpg','png','jpeg');

   			 $new = $name.'.'.$suffix;

   			if(!in_array($suffix,$arr)){
   				return back();
   			}

   			$request->file('img')->move('./consult',$new);
   		}

   		
   		if(!empty($new)){
			$res['img'] = $new;
		}else{
			return back();
		}

		
		$ress=DB::table('consultation')->insert($res);

		if($ress){
			return redirect('/admin/consul/list');
		}else{
			return back();
		}

    }

    //资讯的列表
    public function getList(){

    $res = DB::table("consultation")->paginate(3);

    // dd($res);
  	 return  view('Admin.consul.consultlist',['list'=>$res]);
    }

    public function getDelete($id){

    $res = DB::table('consultation')->where('id',$id)->delete();

	    if($res){
	    	return redirect('/admin/consul/list');	
	    }else{
	    	return redirect('/admin/consul/list');
	    }

    }

    //资讯的修改
    public function getUpdate($id){

    $res = DB::table('consultation')->where('id',$id)->first();
 	
 	 return view('admin.consul.consultupdate',['upda'=>$res]);		
    }
    //执行修改操作
    public function postUpdate(Request $request){

    	$res = $request->all();	
    	
    	
    	$res = $request->only('title','content','text');
   		$id= $request->only('id');

   		//资讯对上传图片的处理
   		if($request->hasFile('img')){

   			$name = rand(1,9999).time();

   			$suffix=$request->file('img')->getClientOriginalExtension();

   			$arr=array('jpg','png','jpeg');

   			 $new = $name.'.'.$suffix;

   			if(!in_array($suffix,$arr)){
   				return back();
   			}

   			$request->file('img')->move('./consult',$new);
   		}

   		
   		if(!empty($new)){
			$res['img'] = $new;
		}else{
			return back();
		}

		
		$ress=DB::table('consultation')->where('id',$id)->update($res);

		if($ress){
			return redirect('/admin/consul/list');
		}else{
			return back();
		}

    }

}
